<?php
namespace app\index\controller;

use think\Db;
use think\Session;
use app\common\controller\Base;
/**
* 
*/
class Register extends Base
{
	
	public function index(){
		$map = input('param.');
        if( !$this->data_verify($map,"pid")){
            $this->error("你的操作有误");
        }
        $logo = Db::name('config')->where('key','web_logo')->value('value');
        $this->assign("logo",$logo);
        $this->assign('Arr',$map);
		return $this->fetch();
	}

    /*public function index(){
            $id = input('param.');
            $pid = isset($id['pid']) ? $id['pid']:'';
            $this->assign('pid',$pid);
        return $this->fetch('login/register');
    }*/

	public function reg_code(){
        if( request()->isAjax() ){

            $phone = input('post.phone');
            $code = rand(100000,999999);
            //加载common目录下的TransmitSms
            $sms = app_service("common","TransmitSms");
            $pd = $sms->phone_validate($phone,$code);

            if($pd){
                Session::set('verify_code',$code);
                $retArr = [
                    "result" => 0,
                    "msg" => "发送成功",
                ];
            }else{
                $retArr = [
                    "result" => 0,
                    "msg" => "发送失败",
                ];
            }
            $this->json_return($retArr);
        }
        
    }

    public function register(){

        if( request()->isAjax() ){

            $map = input('post.');

            if( !$this->data_verify($map,"account,pwd,phone,pid")){
                $retArr = [
                    'result' => 3,
                    'msg' => '你的操作有误',
                ];
                $this->json_return($retArr);
            }
           $verify_code = Session::get('verify_code');
           
           if(intval($map['verify_code']) != $verify_code){
               $retArr = [
                   'result' => 3,
                   'msg' => '你的验证码有误',
               ];
               $this->json_return($retArr);
           }

            $pdArr = Db::name("User")->where("id",$map["pid"])->value("phone");
            
            if( empty($pdArr)){
                $retArr = [
                    'result' => 3,
                    'msg' => '你的PID不存在!',
                ];
                $this->json_return($retArr);
            }

            $account = Db::name('user')->field("id")->where("account",$map["account"])->find();
            if( !empty($account) ){
                $retArr = [
                    'result' => 3,
                    'msg' => '你输入的账号已存在!',
                ];
                $this->json_return($retArr);
            }

            if( isset($map["tuijian"]) && !empty($map["tuijian"]) ){
                $tuijian_id = Db::name("User")->where("account",$map["tuijian"])->value("id");
                if( empty($tuijian_id) ){
                    $retArr = [
                        'result' => 3,
                        'msg' => '你输入的推荐账号不存在!',
                    ];
                    $this->json_return($retArr);
                }
            }else{
                $tuijian_id = $this->userInfo["id"];
            }

            $pas_salt = getRandChar(6);
            $time = time();

            $data = array(
                'id' => $this->get_userId(),
                'nickname' => 'XLS_'.mt_rand(1,99999999),
                'account' => $map['account'],
                'phone' => $map['phone'],
                'portrait' => '/static/index/img/wo.png',
                'password' => getMd5Pas($map["pwd"], $pas_salt),
                'pwd_code' => $pas_salt,
                'add_time' => $time,
                'login_time' => $time,
                'login_ip' => get_client_ip()
            );
            $uid = Db::name('user')->insert($data,false,true);
            if(!empty($uid)){
                $userMsg = [
                    "user_id" => $uid,
                    "parent_id" => $map['pid'],
                ];
                Db::name("userInfo")->insert($userMsg,false,true);
                $arr = [
                    'result' => 0,
                    'msg' => '注册成功',
                ];
	        }else{
                $arr = [
                    'result' =>10004,
                    'msg' => '注册失败',
                ];
	        }
	        $this->json_return($arr);
        }
    }

    private function get_userId(){
        $userId = 0;
        for(;;){
            $userId = mt_rand(1000,99999999);
            $Arr = Db::name("User")->where("id",$userId)->field("id")->find();
            if( empty($Arr) ){
                break;
            }
        }
        return $userId;
    }
}